package data_structure.mooc;

import data_structure.implement.DequeImp;

/**
 * A deque based on the "Array" defined myself
 * @param <E>
 */
public class ArrayDeque<E> implements DequeImp<E> {

    private Array<E> data;

    /**
     * default capacity is ten
     * @param capacity
     */
    public ArrayDeque(int capacity){
        data = new Array<E> (capacity);
    }

    public ArrayDeque(){
        this(10);
    }
    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    @Override
    public void addFront(E e) {
        data.addFirst (e);
    }

    @Override
    public void addLast(E e) {

        data.addLast(e);
    }

    @Override
    public E removeFront() {
        return data.removeFirst();
    }

    @Override
    public E removeLast() {
        return data.removeLast();
    }

    @Override
    public E getFront() {
        return data.getFirst ();
    }

    @Override
    public E getLast() {
        return data.getLast ();
    }

    @Override
    public int getSize() {
        return data.getSize ();
    }

    @Override
    public int getCapacity() {
        return data.getCapacity();
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder ();
        sb.append (String.format ("Deque size=%d capacity=%d", getSize (), getCapacity ()));
        sb.append (" front " +
                           "[");
        for(int i=0;i<getSize(); i++){
            sb.append (data.get (i));
            if(i!=getSize ()-1){
                sb.append (",");
            }
        }
        sb.append ("] tail");
        return sb.toString ();
    }

    public static void main(String[] args) {
        ArrayDeque<Integer>deque=new ArrayDeque<> ();
        for (int i = 0; i < deque.getCapacity (); i++) {
            if(i%2==1){
                deque.addFront (i);
            }else{
                deque.addLast (-i);
            }
            System.out.println(deque.toString ());
        }
        deque.removeFront ();
        System.out.println(deque.toString());
        deque.removeLast ();
        System.out.println(deque.toString());

    }
}
